<!--
 * @Descripttion: (agv信息/base_agv)
 * @Author: (admin)
 * @Date: (2025-04-20)
-->
<template>
	<div>
		<el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
			<el-form-item label="小车编号" prop="agvCode">
				<el-input v-model="queryParams.agvCode" placeholder="请输入小车编号" />
			</el-form-item>
			<el-form-item label="小车状态" prop="statusString">
				<el-input v-model="queryParams.statusString" placeholder="请输入小车状态" />
			</el-form-item>
			<el-form-item>
				<el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
				<el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
			</el-form-item>
		</el-form>
		<!-- 工具区域 -->
		<el-row :gutter="15" class="mb10">
			<el-col :span="1.5">
				<el-button type="primary" v-hasPermi="['base:agv:add']" plain icon="plus" @click="handleAdd">
					{{ $t('btn.add') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" :disabled="single" v-hasPermi="['base:agv:edit']" plain icon="edit"
					@click="handleUpdate">
					{{ $t('btn.edit') }}
				</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" :disabled="multiple" v-hasPermi="['base:agv:delete']" plain icon="delete"
					@click="handleDelete">
					{{ $t('btn.delete') }}
				</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
		</el-row>

		<el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
			highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="50" align="center" />
			<el-table-column prop="agvId" label="主键" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('agvId')" />
			<el-table-column prop="agvCode" label="小车编号" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('agvCode')" />
			<el-table-column prop="online" label="是否在线" width="120" align="center" v-if="columns.showColumn('online')">
				<template #default="scope">
					<el-tag type="success" v-if="scope.row.online == true">是</el-tag>
					<el-tag type="danger" v-else>否</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="taskOnline" label="任务是否在线" width="120" align="center"
				v-if="columns.showColumn('taskOnline')">
				<template #default="scope">
					<el-tag type="success" v-if="scope.row.taskOnline == true">是</el-tag>
					<el-tag type="danger" v-else>否</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="statusString" label="小车状态" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('statusString')" />
			<el-table-column prop="categoryId" label="车型" width="120" align="center"
				v-if="columns.showColumn('categoryId')" />
			<el-table-column prop="mapName" label="地图名称" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('mapName')" />
			<el-table-column prop="taskId" label="任务号" width="120" align="center" v-if="columns.showColumn('taskId')" />
			<el-table-column prop="controlString" label="控制状态" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('controlString')" />
			<el-table-column prop="emergencyStopString" label="是否急停" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('emergencyStopString')" />
			<el-table-column prop="goodsStatusString" label="有无货" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('goodsStatusString')" />
			<el-table-column prop="power" label="电量" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('power')" />
			<el-table-column prop="softStop" label="是否软急停" width="120" align="center" v-if="columns.showColumn('softStop')" />
			<el-table-column prop="siteId" label="点位" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('siteId')" />
			<el-table-column prop="angle" label="车身角度" width="120" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('angle')" />
			<el-table-column prop="x" label="x" width="100" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('x')" />
			<el-table-column prop="y" label="y" width="100" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('y')" />
			<el-table-column prop="xy" label="xy" width="100" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('xy')" />
			<el-table-column prop="speed" label="速度" width="100" align="center" v-if="columns.showColumn('speed')" />
			<el-table-column prop="errCode" label="故障码" width="100" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('errCode')" />
			<el-table-column prop="warningPowerLow" label="预警电量" width="100" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('warningPowerLow')" />
			<el-table-column prop="additiveInfo" label="AdditiveInfo" width="100" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('additiveInfo')" />
			<el-table-column prop="remark" width="120" label="备注" align="center" :show-overflow-tooltip="true"
				v-if="columns.showColumn('remark')" />
			<el-table-column label="操作" min-width="160" fixed="right">
				<template #default="scope">
					<el-button-group>
						<el-button text v-hasPermi="['base:agv:edit']" type="success" icon="edit" @click="handleUpdate(scope.row)">
							{{ $t('btn.edit') }}
						</el-button>
						<el-button text v-hasPermi="['base:agv:delete']" type="danger" icon="delete"
							@click="handleDelete(scope.row)">
							{{ $t('btn.delete') }}
						</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</el-table>
		<pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
			@pagination="getList" />


		<el-dialog draggable :close-on-click-modal="false" :title="title" :lock-scroll="false" v-model="open">
			<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
				<el-row :gutter="20">
					<el-col :lg="12">
						<el-form-item label="主键" prop="agvId">
							<el-input v-model="form.agvId" placeholder="请输入主键" :disabled="opertype != 1" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="小车编号" prop="agvCode">
							<el-input v-model="form.agvCode" placeholder="请输入小车编号" />
						</el-form-item>
					</el-col>
					<el-col :lg="6">
						<el-form-item label="是否在线" prop="online">
							<el-switch v-model="form.online" />
						</el-form-item>
					</el-col>
					<el-col :lg="6">
						<el-form-item label="任务是否在线" prop="taskOnline" label-width="120px">
							<el-switch v-model="form.taskOnline" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="小车状态" prop="statusString">
							<el-input v-model="form.statusString" placeholder="请输入小车状态" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="车型" prop="categoryId">
							<el-input v-model.number="form.categoryId" placeholder="请输入车型" />
						</el-form-item>
					</el-col>

					<el-col :lg="12">
						<el-form-item label="地图名称" prop="mapName">
							<el-input v-model="form.mapName" placeholder="请输入地图名称" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="任务号" prop="taskId">
							<el-input v-model.number="form.taskId" placeholder="请输入任务号" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="控制状态" prop="controlString">
							<el-input v-model="form.controlString" placeholder="请输入控制状态" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="是否急停" prop="emergencyStopString">
							<el-input v-model="form.emergencyStopString" placeholder="请输入是否急停" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="有无货" prop="goodsStatusString">
							<el-input v-model="form.goodsStatusString" placeholder="请输入有无货" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="电量" prop="power">
							<el-input v-model="form.power" placeholder="请输入电量" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="是否软急停" prop="softStop">
							<el-input v-model.number="form.softStop" placeholder="请输入是否软急停" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="点位" prop="siteId">
							<el-input v-model="form.siteId" placeholder="请输入点位" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="车身角度" prop="angle">
							<el-input v-model="form.angle" placeholder="请输入车身角度" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="x" prop="x">
							<el-input v-model="form.x" placeholder="请输入x" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="y" prop="y">
							<el-input v-model="form.y" placeholder="请输入y" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="xy" prop="xy">
							<el-input v-model="form.xy" placeholder="请输入xy" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="速度" prop="speed">
							<el-input v-model.number="form.speed" placeholder="请输入速度" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="故障码" prop="errCode">
							<el-input v-model="form.errCode" placeholder="请输入故障码" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="预警电量" prop="warningPowerLow">
							<el-input v-model="form.warningPowerLow" placeholder="请输入预警电量" />
						</el-form-item>
					</el-col>
					<el-col :lg="12">
						<el-form-item label="AdditiveInfo" prop="additiveInfo">
							<el-input v-model="form.additiveInfo" placeholder="请输入AdditiveInfo" />
						</el-form-item>
					</el-col>
					<el-col :lg="24">
						<el-form-item label="备注" prop="remark">
							<el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer v-if="opertype != 3">
				<el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
				<el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="baseagv">
import {
	pageBaseAgv,
	addBaseAgv, delBaseAgv,
	updateBaseAgv, getBaseAgv,
}
	from '@/api/baseManage/baseAgv.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
	pageNum: 1,
	pageSize: 10,
	sort: '',
	sortType: 'asc',
	agvCode: undefined,
	statusString: undefined,
})
const columns = ref([
	{ visible: false, prop: 'agvId', label: '主键' },
	{ visible: true, prop: 'agvCode', label: '小车编号' },
	{ visible: true, prop: 'online', label: '是否在线' },
	{ visible: true, prop: 'taskOnline', label: '任务是否在线' },
	{ visible: true, prop: 'statusString', label: '小车状态' },
	{ visible: true, prop: 'categoryId', label: '车型' },
	{ visible: true, prop: 'mapName', label: '地图名称' },
	{ visible: true, prop: 'taskId', label: '任务号' },
	{ visible: true, prop: 'controlString', label: '控制状态' },
	{ visible: true, prop: 'emergencyStopString', label: '是否急停' },
	{ visible: true, prop: 'goodsStatusString', label: '有无货' },
	{ visible: true, prop: 'power', label: '电量' },
	{ visible: false, prop: 'softStop', label: '是否软急停' },
	{ visible: false, prop: 'siteId', label: '点位' },
	{ visible: false, prop: 'angle', label: '车身角度' },
	{ visible: false, prop: 'x', label: 'x' },
	{ visible: false, prop: 'y', label: 'y' },
	{ visible: false, prop: 'xy', label: 'xy' },
	{ visible: false, prop: 'speed', label: '速度' },
	{ visible: true, prop: 'errCode', label: '故障码' },
	{ visible: false, prop: 'warningPowerLow', label: '预警电量' },
	{ visible: false, prop: 'additiveInfo', label: 'AdditiveInfo' },
	{ visible: false, prop: 'remark', label: '备注' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList() {
	loading.value = true
	pageBaseAgv(queryParams).then(res => {
		const { code, data } = res
		if (code == 200) {
			dataList.value = data.result
			total.value = data.totalNum
			loading.value = false
		}
	})
}

// 查询
function handleQuery() {
	queryParams.pageNum = 1
	getList()
}

// 重置查询操作
function resetQuery() {
	proxy.resetForm("queryRef")
	handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
	ids.value = selection.map((item) => item.agvId);
	single.value = selection.length != 1
	multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
	var sort = undefined
	var sortType = undefined

	if (column.prop != null && column.order != null) {
		sort = column.prop
		sortType = column.order

	}
	queryParams.sort = sort
	queryParams.sortType = sortType
	handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
	single: true,
	multiple: true,
	form: {},
	rules: {
		agvId: [{ required: true, message: "主键不能为空", trigger: "blur" }],
		agvCode: [{ required: true, message: "小车编号不能为空", trigger: "blur" }],
		online: [{ required: true, message: "是否在线不能为空", trigger: "blur" }],
		taskOnline: [{ required: true, message: "任务是否在线不能为空", trigger: "blur" }],
	},
	options: {
	}
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
	open.value = false
	reset()
}

// 重置表单
function reset() {
	form.value = {
		agvId: null,
		agvCode: null,
		online: null,
		taskOnline: null,
		statusString: null,
		categoryId: null,
		mapName: null,
		taskId: null,
		controlString: null,
		emergencyStopString: null,
		goodsStatusString: null,
		power: null,
		softStop: null,
		siteId: null,
		angle: null,
		x: null,
		y: null,
		xy: null,
		speed: null,
		errCode: null,
		warningPowerLow: null,
		additiveInfo: null,
		createBy: null,
		createTime: null,
		updateBy: null,
		updateTime: null,
		remark: null,
	};
	proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
	reset();
	open.value = true
	title.value = '添加agv信息'
	opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
	reset()
	const id = row.agvId || ids.value
	getBaseAgv(id).then((res) => {
		const { code, data } = res
		if (code == 200) {
			open.value = true
			title.value = '修改agv信息'
			opertype.value = 2

			form.value = {
				...data,
			}
		}
	})
}

// 添加&修改 表单提交
function submitForm() {
	proxy.$refs["formRef"].validate((valid) => {
		if (valid) {

			if (form.value.agvId != undefined && opertype.value === 2) {
				updateBaseAgv(form.value).then((res) => {
					proxy.$modal.msgSuccess("修改成功")
					open.value = false
					getList()
				})
			} else {
				addBaseAgv(form.value).then((res) => {
					proxy.$modal.msgSuccess("新增成功")
					open.value = false
					getList()
				})
			}
		}
	})
}

// 删除按钮操作
function handleDelete(row) {
	const Ids = row.agvId ? [row.agvId] : undefined || ids.value
	proxy
		.$confirm('是否确认删除"' + Ids.length + '"条数据项？', "警告", {
			confirmButtonText: proxy.$t('common.ok'),
			cancelButtonText: proxy.$t('common.cancel'),
			type: "warning",
		})
		.then(function () {
			return delBaseAgv(Ids)
		})
		.then(() => {
			getList()
			proxy.$modal.msgSuccess("删除成功")
		})
}




handleQuery()
</script>